package org.simp.router;

import org.simp.common.Constants;
import org.simp.core.message.SimpMessage;
import org.simp.core.request.SimpRequest;
import org.simp.handler.SimpHandler;
import org.simp.node.SimpClientConnector;
import org.simp.node.SimpNode;
/**
 * 自上而下的消息使用这个拦截器，将消息转发给下层的服务器或者客户端
 * @author zhuhaitao at 2014-5-28
 *
 */
public class ClientRouterInterceptor implements RouterInterceptor{

	@Override
	public boolean dispatchRequest(SimpRequest simpRequest, SimpHandler handler) {
		String contextName = handler.getContext().getContextName();
		String objAddress = simpRequest.getObjAddress();
		
		if(objAddress.endsWith(contextName) && objAddress.contains(Constants.ADDRESS_SPLIT)){
			dispatchSimpMessage(simpRequest.getSimpMessage(), handler);
			return true;
		}
		return false;
	}

	@Override
	public void dispatchSimpMessage(SimpMessage message, SimpHandler handler) {
		SimpNode node =  handler.getContext().getSimpNode();
		message.decObjAddress();
		node.sendToDownNode(message);
	}

}
